$(function() {
	var error_check = function() {
		var $form = $(this);
		var $test_input = $('<input type="hidden" name="test" value="1"/>');
		var $div = $('<div/>');

		$form.append($test_input);
		$('span.error', $form).each(function() {
			var $span = $(this);
			$span.hide('slow', function() { $span.remove() });
		});

		$div.load($form.attr('action') + ' #form', $form.serializeArray(), function() {
			$ps = $('p.error', $div);

			if ( $ps.length ) {
				//location.hash = $ps.attr('id');
				$ps.each(function() {
					var $p = $(this);
					var $span = $('span.error', $p);
					var $target_p = $('#' + $p.attr('id'));
					var $input = ('input, select, textarea', $target_p);
					$span.css({opacity: 0});
					$target_p.prepend($span);
					$span.animate({opacity: 1}, 'slow');
					$input.bind('change', function() {
						$span.hide('slow', function() { $span.remove() });
					});
				});
			} else {
				$form.unbind('submit', error_check);
				$form.submit();
			}
		});

		$test_input.remove();
		return false;
	};

	$('form').bind('submit', error_check);

	var $navs = $('#nav > ul > li').each(function() {
		var me = this;
		var $this = $(this);
		$('input', this)
			.bind('focus', function() { $this.addClass('focus') })
			.bind('blur', function() { $this.removeClass('focus') });

		$this
			.bind('mouseover', function() {
				$navs.each(function() {
					if ( this != me ) {
						$(this).removeClass('hover').removeClass('focus');
					}
				});

				$(this).addClass('hover');
			})
			.bind('mouseout', function() { $(this).removeClass('hover') });
	});

	//CODE
	$('.code .ubb a').bind('click', function() {
		$('body')
			.removeClass('ubb')
			.addClass('html');
		$.mysmilies.blindGet(this.href);
		return false;
	});

	$('.code .html a').bind('click', function() {
		$('body')
			.removeClass('html')
			.addClass('ubb');
		$.mysmilies.blindGet(this.href);
		return false;
	});
	// /CODE

	// FAVORITE
	var load_faves = function(href) {
		href += ( (href.indexOf('?') != -1) ? '&' : '?' ) + 'ajax=1';
		var $div = $('#favorites');
		$div.animate({opacity: 0});
		$div.sniffLoad(href + ' #favorites', function() {
			$div.animate({opacity: 1});
		});
	};

	$('.add_favorite a').bind('click', function() {
		var $this = $(this);
		var success = load_faves(this.href);
		if ( success ) $('.smiley').has($this).addClass('favorite');
		return false;
	});

	$('.remove_favorite a').bind('click', function() {
		var $this = $(this);
		var success = load_faves(this.href);
		if ( success ) $('.smiley').has($this).removeClass('favorite');
		return false;
	});
	// /FAVORITE

	// SMILEY
	$('.smiley .html input, .smiley .ubb input')
		.live('focus', function() {
			this.select();
		})
		.live('click', function() {
			this.select();
			if ( document.execCommand ) {
				alert(document.execCommand('Copy'));
			}
		});
	// /SMILEY

	// TAG
	$('.add_vote').bind('click', function() {
		var $this = $(this);
		$.mysmilies.sniffGet(this.href, function() {
			$('.vote').has($this).addClass('voted');
		});
		
		return false;
	});

	$('.remove_vote').bind('click', function() {
		var $this = $(this);
		$.mysmilies.sniffGet(this.href, function() {
			$('.vote').has($this).removeClass('voted');
		});
		return false;
	});
	// /TAG

	$('html').addClass('scriptable');
	for ( var key in $.browser ) {
		$('html').addClass(key);
		break;
	}
	/*$('.carousel_window')
		.carousel()
		.each(function() {
			var $this = $(this);
			var doit = function() {
				var offset = $this.offset();
				var $div = $('<div/>').append($('<p/>').html('D<br/>r<br/>a<br/>g')).addClass('carousel_alert').css({position: 'absolute'});
				$('body').append($div);
				$div.css({
					left: offset.left + $this.outerWidth() - $div.outerWidth(),
					top: offset.top,
					height: $this.outerHeight() - parseInt($div.css('borderTopWidth')) - parseInt($div.css('borderBottomWidth')) - parseInt($div.css('paddingTop')) - parseInt($div.css('paddingBottom'))
				});
				$div.animate({left: offset.left + $this.outerWidth() - $div.outerWidth() - 200, opacity: 0}, 2000, function() {
					$div.remove();
					delete $div;
				})
				$this.unbind('mouseover', doit);
			};

			$this.bind('mouseover', doit);
		});*/

	var posts_ajax = function() {
		var $posts = $(this);

		$('a', $('> .pagination', this))
			.bind('click', function() {
				var $this = $(this);
				var href = $this.attr('href');
				href += ( (href.indexOf('?') != -1) ? '&' : '?' ) + 'ajax=1';
				var $response = $('<div/>');
				$response.load(href, function() {
					$posts.html($('.posts', $response).html());
					$posts.each(posts_ajax);
				});
				return false;
			});
	};

	$('.posts').each(posts_ajax);
});

(function($) {
	$.mysmilies = {
		modalOptions: {
			width: null,
			height: null
		},
		blindGet: function(href) {
			href += ( (href.indexOf('?') != -1) ? '&' : '?' ) + 'ajax=1';
			$.get(href);
		},
		sniffGet: function(href, selector, callback) {
			href += ( (href.indexOf('?') != -1) ? '&' : '?' ) + 'ajax=1';
			if ( typeof(selector) == 'function' ) {
				callback = selector;
				selector = '';
			}

			if ( selector ) selector = ' ' + selector;

			var $div = $('<div/>');
			$div.load(href + selector, function() {
				var fail = false;
				if ( $div.has('#login_required').length ) {
					$('#login_required', $div).showModal($.mysmilies.modalOptions);
					fail = true;
				}
				delete $div;
				if ( !fail && callback ) {
					callback();
				}
			});
		}
	};

	$.fn.sniffLoad = function(href, data, callback) {
		var $this = $(this);
		if ( typeof(data) == 'function' ) {
			callback = data;
			data = null;
		}

		var $div = $('<div/>');

		$div.load(href, data, function(text) {
			var fail = false;
			$div.html(text);

			if ( $('#login_required', $div).length ) {
				$('#login_required', $div).showModal($.mysmilies.modalOptions);
				fail = true;
			}
			if ( !fail ) {
				$this.html($div.html());
				if ( callback ) callback();
			}
			delete $div;
		});
	};
})(jQuery);